<?php

namespace app\checkbill\logic;

use app\common\library\RedissLock;
use app\common\service\CommonSystemImportService;
use think\facade\Db;
use Throwable;
use app\checkbill\model\Checkbill as CheckbillModel;

class CheckbillLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new CheckbillModel();
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 订单列表
     * @author zh
     */
    public function getcheckbillsList($params = [])
    {
        $limit  = $params['limit'] ?? 10;
        $name  = $params['name'] ?? "";
        $where = [];
        $order = ['createtime' => 'desc', 'id' => 'desc'];
        if (!empty($name)) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $res = $this->model
            // ->withJoin(['appends'], 'left')
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $data = [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => '',
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }

    public function addcheckbill($data)
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }

        $result = false;
        $this->model->startTrans();
        try {
            $result = $this->model->insert($data);
            $this->model->commit();
        } catch (Throwable $e) {
            $this->model->rollback();
            return json(DefaultResponse(201, false, $e->getMessage(), null));
        }
        if ($result !== false) {
            return json(DefaultResponse(200, true, '添加成功!', null));
        } else {
            return json(DefaultResponse(201, false, '添加失败!', null));
        }
    }

    public function editcheckbill($project, $data)
    {
        if (!$data) {
            return json(DefaultResponse(201, false, '参数不能为空!', null));
        }

        $result = false;
        $this->model->startTrans();
        try {
            $result = $project->save($data);
            $this->model->commit();
        } catch (Throwable $e) {
            $this->model->rollback();
            return json(DefaultResponse(201, false, $e->getMessage(), null));
        }
        if ($result !== false) {
            return json(DefaultResponse(200, true, '更新成功!', null));
        } else {
            return json(DefaultResponse(201, false, '更新失败!', null));
        }
    }











}
